package com.lfy.yunpicture.manager.websocket.disruptor;

import com.lfy.yunpicture.common.ErrorCode;
import com.lfy.yunpicture.exception.ThrowUtils;
import com.lfy.yunpicture.manager.websocket.PictureEditWorkHandle;
import com.lfy.yunpicture.manager.websocket.model.PictureEditEvent;
import com.lfy.yunpicture.manager.websocket.model.PictureEditRequestMessage;
import com.lfy.yunpicture.manager.websocket.model.PictureEditTypeEnum;
import com.lmax.disruptor.WorkHandler;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import javax.annotation.Resource;

/**
 * 图片编辑事件消费者
 */
@Component
public class PictureEditEventWorkHandle implements WorkHandler<PictureEditEvent>{
    @Resource
    @Lazy
    private PictureEditWorkHandle pictureEditWorkHandle;
    @Override
    public void onEvent(PictureEditEvent event) throws Exception {
        PictureEditRequestMessage pictureEditRequestMessage = event.getPictureEditRequestMessage();
        Long pictureId = event.getPictureId();
        WebSocketSession session = event.getWebSocketSession();
        String type = pictureEditRequestMessage.getType();
        PictureEditTypeEnum enumByValue = PictureEditTypeEnum.getEnumByValue(type);
        ThrowUtils.throwIf(enumByValue == null, ErrorCode.PARAMS_ERROR);
        switch (enumByValue) {
            case ENTER_EDIT:
                pictureEditWorkHandle.handleEnterEdit(pictureId, session, pictureEditRequestMessage);
                break;
            case EXIT_EDIT:
                pictureEditWorkHandle.handleExitEdit(pictureId, session, pictureEditRequestMessage);
                break;
            case ACTION_EDIT:
                pictureEditWorkHandle.handleActionEdit(pictureId, session, pictureEditRequestMessage);
                break;
            default:
                break;
        }
    }
}
